package pers.lan.stack;

import java.util.Arrays;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/5 4:39 PM
 */
public class LC_1019 {

    public static void main(String[] args) {
        ListNode head = new ListNode(2);
        head.next = new ListNode(1);
        head.next.next = new ListNode(5);

        System.out.println(
                Arrays.toString(
                        new LC_1019().nextLargerNodes(head)
                )
        );
    }

    public int[] nextLargerNodes(ListNode head) {
        int[] stack = new int[10000];
        int[] res = new int[10000];
        int[] temp = new int[10000];
        int top = -1, i = 0;
        ListNode node = head;
        while (node != null) {
            while (top != -1 && temp[stack[top]] < node.val)
                res[stack[top--]] = node.val;
            stack[++top] = i;
            temp[i++] = node.val;
            node = node.next;
        }
        return Arrays.copyOf(res, i);
    }

    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
}
